package org.example.myleet.p1260;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<List<Integer>> shiftGrid(int[][] grid, int k) {
        int m = grid.length, n = grid[0].length, total = m * n;
        int[] cache = new int[total];
        for (int i = 0; i < m; ++i) {
            int offset = i * n + k;
            for (int j = 0; j < n; ++j) {
                cache[(offset + j) % total] = grid[i][j];
            }
        }
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < m; ++i) {
            List<Integer> row = new ArrayList<>();
            int offset = i * n;
            for (int j = 0; j < n; ++j) {
                row.add(cache[offset + j]);
            }
            res.add(row);
        }
        return res;
    }
}
